package com.tanhua.server.service;

import com.tanhua.commons.template.HuanXinTemplate;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mogo.Comment;
import com.tanhua.domain.mogo.Friend;
import com.tanhua.domain.vo.ContactVO;
import com.tanhua.domain.vo.MessageCommentVO;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.CommentApi;
import com.tanhua.dubbo.api.FriendApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MessageService {
    @Reference
    private CommentApi commentApi;
    @Reference
    private UserInfoApi userInfoApi;

    @Reference
    private FriendApi friendApi;
    @Autowired
    private HuanXinTemplate huanXinTemplate;


    //消息--添加联系人（聊一下  添加好友）
    public ResponseEntity addContacts(long targetUserId) {
        //把好友关系保存到mongoDB
        friendApi.addFriend(UserHolder.getUserId(), targetUserId);
        //把好友关系注册到环信里
        huanXinTemplate.addContactUser(UserHolder.getUserId(), targetUserId);

        return ResponseEntity.ok(null);
    }

    //消息--联系人列表
    public ResponseEntity findContactList(int page, int pagesize, String keyword) {
        //查询当前用户的好友列表
        PageResult<Friend> pageResult = friendApi.findFriendList(page, pagesize, keyword, UserHolder.getUserId());

        //转换vo
        List<Friend> friendList = pageResult.getItems();
        List<ContactVO> voList = new ArrayList<>();
        for (Friend friend : friendList) {
            ContactVO vo = new ContactVO();
            UserInfo userInfo = userInfoApi.findById(friend.getFriendId());
            BeanUtils.copyProperties(userInfo, vo);
            vo.setUserId(userInfo.getId().toString());
            vo.setCity(StringUtils.substringBefore(userInfo.getCity(), "-"));
            voList.add(vo);
        }
        //构造返回值
        PageResult<ContactVO> voPageResult = new PageResult<>();
        BeanUtils.copyProperties(pageResult, voPageResult);
        voPageResult.setItems(voList);

        return ResponseEntity.ok(voPageResult);
    }
///**
// * 查询当前用户的评论列表
// * @param commentType 评论类型 1点赞，2评论，3喜欢
// * @param page 页码
// * @param pagesize 每页几条
// * @return
// */

    public ResponseEntity findCommentList(int commentType, int page, int pagesize) {
        //查询针对当前用户的评论列表
        PageResult<Comment> pageResult = commentApi.findCommentsByUser(UserHolder.getUserId(), commentType, page, pagesize);
        //转换成vo
        List<Comment> commentList = pageResult.getItems();
        List<MessageCommentVO> voList = new ArrayList<>();
        for (Comment comment : commentList) {
            MessageCommentVO vo = new MessageCommentVO();
            UserInfo userInfo = userInfoApi.findById(comment.getUserId());
            BeanUtils.copyProperties(userInfo, vo);
            vo.setId(userInfo.getId().toString());
            String dateStr = new SimpleDateFormat("yyyy年MM月dd日HH:mm").format(new Date(comment.getCreated()));
            vo.setCreateDate(dateStr);
            voList.add(vo);

        }
        //构造返回值
        PageResult<MessageCommentVO> voPageResult = new PageResult<>();
        BeanUtils.copyProperties(pageResult, voPageResult);
        voPageResult.setItems(voList);
        return ResponseEntity.ok(voPageResult);
    }

}
